Lua templating/InfoboxBuilder
The Lua InfoboxBuilder greatly simplifies the creation of infoboxes. What's more, it makes pages render faster than those that use infoboxes built with traditional wikitext. Improvements in rendering time are increasingly obvious with more complicated infoboxes. While any community would benefit from using Lua, you should definitely consider using the Lua InfoboxBuilder if your infoboxes contain nested templates. Because it uses Lua, a fully-fledged programming language, the InfoboxBuilder allows you to create infoboxes that are as complicated as you wish — based upon a highly simplified, standardized core. No matter where you are on the Fandom network, you'll recognize the "guts" of the Lua Infobox, but you won't be a slave to its simplicity. The Lua InfoboxBuilder offers you an easy way to create infoboxes, without losing the ability to make them completely your own. What do I need? The InfoboxBuilder is now a parser function and is enabled automatically. Creating a simple infobox If you want to create a simple infobox - it is really easy to do. The source code of the one on the right is: InfoboxBuilder comes with a default theme which is meant to look good on any background. The syntax InfoboxBuilder uses the new syntax which is meant to be simple, yet descriptive. The general pattern for each argument is: | Index:Key = Value Every row must have a numerical index that defines its place in the infobox and groups the arguments. The basic ones are Type and Value. You can use the following Types: * Title * Header * Line * Image * MainImage * Footer * Custom Line and Custom require using a Label argument. For Line it defines a label of a field and for Custom it defines an HTML tag that you want to use as a container (e.g. div, span). You can also use Label to define captions of an Image or a MainImage field. Split type Split type builds on 4 elements. These elements coincide with left and right column, as seen in example below. Just as with other types, you can assign a method to each Label and Value. Custom type Custom type wraps the "value" using a XML or html tag. The example below show the creation of an unordered list: Tweaking an infobox Variables There are several ways you can modify a look or a behavior of your infobox by using Variables arguments. They all have an index of 0. The default ones are: | 0:Theme = default, -- Adds prefix to CSS classes -- (prefix you want to use) | 0:CustomModule = , -- Defines a path to a Lua module with custom functions -- (name of module with a Module: prefix) | 0:MainImageCaption = Off, -- Toggles display of Label in the Main Image field -- (Off/On) | 0:ToggleContentLongerThan = 1000 -- Makes fields with long values collapsible -- (number of characters) To overwrite them just put it in the source of your infobox with a different value: You can also define your own variables, for example if you want to use a magic word add: | 0:Pagename = The pagename of an article using this infobox will be passed now for a later use. Styling All of this would not be worth much if you couldn't style your brand new infobox. Each element of an infobox has a specific class name with a prefix specified in the Theme variable (e.g. if the prefix is "default" a container div will have a class name "default-infobox-container"). On top of that, each type can also have their own CSS class if one type should be different from another as seen in the example below: We recommend to put all of the CSS code in a MediaWiki:Common.css article. You can use the my-theme theme as a starter. Complex infoboxes with custom Lua modules InfoboxBuilder works fine for simple infoboxes, but the real magic happens with complex infoboxes that use a lot of logic. The most important thing here is that you can include your own Lua module that handles the logic. But, first things first. Step 1 - Provide data Infoboxes that already exist When you convert an existing wikitext template to Lua, you don’t lose all the hard work you’ve put into every page that uses it. Say you’ve filled out a wikitext infobox on Batman that looks like this: When you convert to use Lua, you lose none of that data! It's all automatically picked up by the new, Lua-based code. So don't worry: converting to Lua does not mean you're going to have do a ton of work on your site. New infoboxes However, if you write a new template you need some input. In this example we will use this wikitext code: It calls a Template:LuaInfobox article and passes some info about the InfoboxBuilder module. You can find this code in the beginning of this article. Step 2 - Infobox Template At first, we will create a template to model our infobox. To be consistent with the data from Step 1 let's call it Template:LuaInfobox. We will also use the my-theme theme from Common.css. To use a Custom Module, we need to put its name in a 0:CustomModule variable: } | 2:Type = MainImage | 2:Label = Module's logo or Lua logo | 2:Value = } | 3:Type = Header | 3:Label = Basic info | 3:Value = Basic Information | 4:Type = Line | 4:Label = Module's name | 4:Value = } | 5:Type = Line | 5:Label = Dependencies | 5:Value = } | 5:ValueDesc = } | 5:ValueMethod = Dependencies | 6:Type = Line | 6:Label = Version | 6:Value = } }} Step 3 - Custom modules CustomModule = Module:LuaInfobox Custom module's code local HF = require('Module:HF') local LuaInfobox = {} function LuaInfobox.Dependencies( field, vars ) local output = "" local dependencies = HF.explode( ";", field.Value ) for i, dependency in ipairs( dependencies ) do output = output .. "" .. dependency .. "" .. "; " end if string.sub( output, -2, -1 ) "; " then output = string.sub( output, 1, -3 ) -- Remove last semi-colon and space end return output end return LuaInfobox Every method retrieves two arguments - table of a field with names of keys matching the ones from a template (e.g. Type, Label, Value) and a similar table of variables (e.g. Theme, Pagename). As you can see you are able to require other Lua modules so you don't have to store everything in one place. Be careful however, because Lua's engine has a limit of 100 modules included at once. Usage in a template Now we need to instruct InfoboxBuilder to use this method for a Dependency field and it is done by adding a ValueMethod argument with a function's name: | 5:Type = Line | 5:Label = Dependencies | 5:Value = } | 5:ValueMethod = Dependencies There is no naming convention to stick to, however using a module's name for a main variable and fields' names for functions will help you keep things organised. You can also process Labels in the same way. Just add LabelMethod argument with a name of a function you want to use. Multiple values Since every method has access to all arguments grouped in one field you can pass multiple values to it. For example - we want to point that all dependencies are required. We can do it by adding a new argument to the Dependencies field: | 5:Type = Line | 5:Label = Dependencies | 5:Value = } | 5:ValueDesc = } | 5:ValueMethod = Dependencies and define its value in an article: | Dependencies = InfoboxBuilderView; HF | DependenciesDesc = (all are required) Now we can use it to add additional notes to the Dependencies field: function LuaInfobox.Dependencies( field, vars ) local output = "" local dependencies = HF.explode( ";", field.Value ) for i, dependency in ipairs( dependencies ) do output = output .. "" .. dependency .. "" .. "; " end if string.sub( output, -2, -1 ) "; " then output = string.sub( output, 1, -3 ) -- Remove last semi-colon and space end if not HF.isempty( field.ValueDesc ) then output = output .. " " .. field.ValueDesc end return output end